package com.example.surveyapplication;

import java.util.ArrayList;
import java.util.List;

import object.ResultObject;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;
import bus.PatientBUS;
import bus.TestBUS;
import bus.TestScoreBUS;
import dto.PatientDTO;
import dto.TestDTO;
import dto.TestScoreDTO;

public class ResultActivity extends Activity implements OnClickListener {
	TextView lblSignOut;
	
	public static ListView lvResult;

	private List<ResultObject> listResultObjects;
	private ResultListViewAdapter resultAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_result);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		
		lblSignOut = (TextView)findViewById(R.id.lblSignOut_Result);		
		lblSignOut.setOnClickListener(this);
		
		//
		lvResult = (ListView) findViewById(R.id.lvResult);
		listResultObjects = new ArrayList<ResultObject>();		
		
		TestBUS testBUS = new TestBUS();
		List<TestDTO> lstTest = new ArrayList<TestDTO>();
		lstTest = testBUS.getAllTests();
		
		PatientBUS patientBUS = new PatientBUS();
		List<PatientDTO> lstPatient = new ArrayList<PatientDTO>();
		lstPatient = patientBUS.getAllPatients();
		
		TestScoreBUS testScoreBUS = new TestScoreBUS();
		List<TestScoreDTO> lstTestScore = new ArrayList<TestScoreDTO>();
		lstTestScore = testScoreBUS.getAllTestScores();
		
		for (int i = 0; i < lstTest.size(); i++) {
			TestDTO test = lstTest.get(i);
			PatientDTO patient = new PatientDTO();
			if (test.getPatientID() == lstPatient.get(i).getID()) {
				patient.update(lstPatient.get(i));
			}
			else {
				for (int j = 0; j < lstPatient.size(); j++) {
					if (lstPatient.get(j).getID() == test.getPatientID()) {
						patient.update(lstPatient.get(j));
					}
				}
			}
			
			List<Integer> lstScore = new ArrayList<Integer>();
			
			for (int k = 0; k < lstTestScore.size(); k++) {
				if (lstTestScore.get(k).getTestID() == test.getID()) {
					lstScore.add(lstTestScore.get(k).getTotalScore());
				}
			}
			
			listResultObjects.add(new ResultObject(String.valueOf(i + 1), patient.getFirstName(), patient.getSurname(), String.valueOf(lstScore.get(0)), String.valueOf(lstScore.get(1)), String.valueOf(lstScore.get(2)), test.getTestDate().toGMTString()));
		}

		resultAdapter = new ResultListViewAdapter(this, listResultObjects);
		lvResult.setAdapter(resultAdapter);	
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.result, menu);
		return true;
	}

	public void onClick(View view) {
		Intent intent;
		switch (view.getId()) {
		case R.id.lblSignOut_Result:
			intent = new Intent(this, SignInActivity.class);
			startActivity(intent);
			finish();
			break;
		default:
			break;
		}
	}
}
